import storage from '@/utils/storage.js';
import { AllMomentListApi, getLabelListApi, getMomentListApi, getStepsApi } from '@/servers/momentServer.js';
import dayjs from 'dayjs';

const momentStore = {
    namespaced: true,
    state: {
        homeMoment: [
            // {
            //     content: "菠萝咕唠肉酸甜口，还有菠萝的香气，好吃~",
            //     id: 1,
            //     imgList: [
            //         {
            //             id: 24,
            //             img_path: "https://ly-sys-image.oss-cn-beijing.aliyuncs.com/img/uploads/20240424/2313f4bfa25ee4b4ba948c1bba66357d.png"
            //         },
            //         {
            //             id: 25,
            //             img_path: "https://ly-sys-image.oss-cn-beijing.aliyuncs.com/img/uploads/20240424/3b85d963b9e3768811ba719a49909f1f.png"
            //         },
            //         {
            //             id: 26,
            //             img_path: "https://ly-sys-image.oss-cn-beijing.aliyuncs.com/img/uploads/20240428/eb0be1b916049f18e0bd1dbc3414a673.png"
            //         }
            //     ],
            //     labelList: [1, 2, 3],
            //     redient: [{name: "猪里脊", weight: "200g"}, {name: "菠萝", weight: "150g"}, {name: "青椒", weight: "50g"}],
            //     title: "RECIPE",
            //     updateTime: "2024-05-04T07:49:28.000Z",
            //     upvoteCount: 3
            // },
            // {
            //     content: "菠萝咕唠肉酸甜口，还有菠萝的香气，好吃~",
            //     id: 1,
            //     imgList: [
            //         {
            //             id: 24,
            //             img_path: "https://ly-sys-image.oss-cn-beijing.aliyuncs.com/img/uploads/20240424/2313f4bfa25ee4b4ba948c1bba66357d.png"
            //         },
            //         {
            //             id: 25,
            //             img_path: "https://ly-sys-image.oss-cn-beijing.aliyuncs.com/img/uploads/20240424/3b85d963b9e3768811ba719a49909f1f.png"
            //         },
            //         {
            //             id: 26,
            //             img_path: "https://ly-sys-image.oss-cn-beijing.aliyuncs.com/img/uploads/20240428/eb0be1b916049f18e0bd1dbc3414a673.png"
            //         }
            //     ],
            //     labelList: [1, 2, 3],
            //     redient: [{name: "猪里脊", weight: "200g"}, {name: "菠萝", weight: "150g"}, {name: "青椒", weight: "50g"}],
            //     title: "RECIPE",
            //     updateTime: "2024-05-04T07:49:28.000Z",
            //     upvoteCount: 3
            // },
            // {
            //     content: "菠萝咕唠肉酸甜口，还有菠萝的香气，好吃~",
            //     id: 1,
            //     imgList: [
            //         {
            //             id: 24,
            //             img_path: "https://ly-sys-image.oss-cn-beijing.aliyuncs.com/img/uploads/20240424/2313f4bfa25ee4b4ba948c1bba66357d.png"
            //         },
            //         {
            //             id: 25,
            //             img_path: "https://ly-sys-image.oss-cn-beijing.aliyuncs.com/img/uploads/20240424/3b85d963b9e3768811ba719a49909f1f.png"
            //         },
            //         {
            //             id: 26,
            //             img_path: "https://ly-sys-image.oss-cn-beijing.aliyuncs.com/img/uploads/20240428/eb0be1b916049f18e0bd1dbc3414a673.png"
            //         }
            //     ],
            //     labelList: [1, 2, 3],
            //     redient: [{name: "猪里脊", weight: "200g"}, {name: "菠萝", weight: "150g"}, {name: "青椒", weight: "50g"}],
            //     title: "RECIPE",
            //     updateTime: "2024-05-04T07:49:28.000Z",
            //     upvoteCount: 3
            // }

        ], // 首页列表
        allMoment: [] , // 全部动态
        labelList:[
            // {name: "美食", id: 1},
            // {name: "hhh", id: 2},
            // {name: "好开心啊", id: 3},
            // {name: "工作", id: 4}
        ], // 标签列表
        stepsList:[
            // {
            //     content: "里脊肉切方块，加生抽1汤匙），盐3g，淀粉3g，油1汤匙（15ml）腌制30分钟；菠萝、青红椒分别切块",id: 1
            // },
            // {
            //     content: "把肉块在面粉里滚一圈，使其均匀的粘一层面粉。放在筛网里抖掉多余的粉",id: 2
            // },
            // {
            //     content: "过油中火炸3-5分钟至8成熟。捞出沥干油备用",id: 3
            // },
            // {
            //     content: "里脊肉切方块，加生抽1汤匙），盐3g，淀粉3g，油1汤匙（15ml）腌制30分钟；菠萝、青红椒分别切块",id: 4
            // },
            // {
            //     content: "把肉块在面粉里滚一圈，使其均匀的粘一层面粉。放在筛网里抖掉多余的粉",id: 5
            // },
            // {
            //     content: "过油中火炸3-5分钟至8成熟。捞出沥干油备用",id: 6
            // }
        ] , // 动态对应的步骤
    },
    mutations: {
        getHomeMoment(state, data) {
            state.homeMoment = data;
        },
        getAllMoment(state, data) {
            state.allMoment = data;
        },
        getLabelList(state,data){
            state.labelList = data
        },
        getStepsList(state,data){
            state.stepsList = data
        }
    },
    actions: {
        async getHomeMomentAction(context){
            const homeOption = {
                home:1 ,
                user_id:1005
            }
            const res = await getMomentListApi(homeOption)
            res.data.forEach(item => {
                item.updateTime = dayjs(item.updateTime).format('MM-DD HH:mm')
                item.labelList = item.labelList.map(labelItem => {
                    labelItem.text = context.state.labelList.filter(l => l.id === labelItem.label_id)[0].name
                    return labelItem
                })
                const user_id = 1005
                item.isUpvote = !!item.upvoteList.filter(item => item.user_id === user_id ).length
            })
            console.log("getHomeMoment" , res.data);
            context.commit("getHomeMoment" , res.data)
        },
        async getAllMomentAction(context){
            const res = await AllMomentListApi()
            res.data.forEach(item => {
                item.updateTime = dayjs(item.updateTime).format('MM-DD HH:mm')
                item.labelList = item.labelList?.map(labelItem => {
                    labelItem.text = context.state.labelList.filter(l => l.id === labelItem.label_id)[0].name
                    return labelItem
                })
                const user_id = storage.getCache("userInfo").user_id
                item.isUpvote = !!item.upvoteList?.filter(item => item.user_id === user_id ).length
            })
            console.log("getAllMoment" , res.data);
            context.commit("getAllMoment" , res.data)
        },
        async getLabelListAction (context){
            const res = await getLabelListApi()
            console.log(res.data,"标签");
            context.commit("getLabelList" , res.data)
        },
        async getStepsListAction (context,data){
            const res = await getStepsApi(data)
            context.commit("getStepsList" , res.data)
        }
    }
}
export default momentStore